home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2808 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.1 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Passing multidimensional arrays to functions in C
  5. Date: 24 Jan 1996 04:00:37 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan23210037@qcd.lanl.gov>
  8. References: <vijay-2301961215460001@vijay.chinalake.navy.mil>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: vijay@nawc690.chinalake.navy.mil's message of Tue, 23 Jan 1996 20:13:31 GMT
  13.  
  14. --text follows this line--
  15. In article <vijay-2301961215460001@vijay.chinalake.navy.mil>
  16. vijay@nawc690.chinalake.navy.mil (Jim Vijay) writes: 
  17. <snip>
  18.    The C FAQ (Q's 6.16 thru 6.20) explains how to pass 2D arrays to functions
  19.    in C.  None of the methods shown is as simple or general as languages such
  20.    as (gasp!) FORTRAN.
  21.  
  22. Different languages are suited for different things, and details like
  23. this often result from subtle differences in viewpoints. I use both
  24. Fortran and C, and am sometimes irritated by small inconveniences like
  25. this. 
  26.  
  27.    While realizing that I'm inviting flames, and realizing this topic
  28.    may have been discussed to death in the past (but not in the FAQ),
  29.  
  30. I remember having participated in a similar discussion once in the
  31. past: and remember having seen it more than that one occassion. It is
  32. a fit topic for discussion, probably even in comp.std.c.
  33.  
  34.    let me ask:
  35.  
  36.       What would break (or even be damaged) in C if the language allowed
  37.       one to pass the width of a 2D array (in general, n-1 dimensions of
  38.       an n dim array) to a function?  It should be easy for a compiler to
  39.       emit code that dynamically depends on these n-1 dimensions. 
  40.       FORTRAN (IV?) at the time of C's birth could do it!
  41.  
  42.    Please note that I'm not asking how the C language defines and uses multi
  43.    dimensional array parameters to functions.  I would like to know _why_ the
  44.    current mechanism was chosen - initially (by K&R?) and later by the ANSI
  45.    C committees.
  46.  
  47. What follows is completely my personal opinion. My knowledge of
  48. history is poor, so take everything with a grain of salt. (I know I
  49. should not violate my own rule of not posting if one does not know:
  50. especially since I know I am getting careless as shown by a lot of my
  51. recent postings; I hope this disclaimer warns you enough.)
  52.  
  53. Originally C was designed in the spirit that it would be a portable
  54. syntax for writing code close to machine language: sort of an high
  55. level assembly language. No assembly language at that time had the
  56. concept of an array: an array therefore was not a basic type in
  57. C. Assembly languages provided means of allocating blocks of memory
  58. ... an array declaration in C originally did precisely that. As a
  59. result array types are somewhat special: there are no array values (or
  60. rvalues), any attempt to use them as an rvalue resulted in a pointer
  61. to the allocated block of memory being used instead. As you could
  62. never pass an array in the first place, how could you hope to specify
  63. the bounds of the array? It may be that this bias towards arrays as
  64. allocation units as opposed to true types contributed to
  65. multidimensional arrays not being treated specially.
  66.  
  67. Today, the rules are somewhat better. Array rvalues are still
  68. absolutely useless: in most uses arrays still become pointers. But,
  69. today, because of the better type checking rules and non-equivalence
  70. of all pointer types (which were probably never specified equivalent,
  71. but often treated that way), arrays are first class types (or at least
  72. one and half class :-). But allowing array sizes to be variables is
  73. still a problem.
  74.  
  75. 1) Currently, according to the ANSI standard, a object allocated `on
  76.    the stack' has little runtime penalty. The compiler
  77.    always knows the exact amount of space it needs for all its local
  78.    variables etc. (excluding of course the heap allocation through
  79.    malloc etc.) Allowing variable sized arrays as a type would mean
  80.    a possible run time allocation. Allowing variable sized arrays
  81.    _only_ in parameter definition would alleviate this
  82.    problem. (FORTRAN chose this rule upto FORTRAN 77)
  83.  
  84. 2) The existence of that type (even if only in parameter
  85.    declarations), would imply changes in the semantics of
  86.    sizeof. Currently sizeof returns a compile time constant and depeds
  87.    on the type of expression, _no_ part of which is evaluated.
  88.  
  89. 3) Allowing the generality of FORTRAN would also imply that the bounds
  90.    declarations be allowed to be forward declarations. This creates
  91.    slight parsing difficulties, and needs resolution of a few
  92.    ambiguities. 
  93.  
  94. Having said all this, do I believe it would be useful? Certainly! This
  95. is onw extension which I use a lot, even though ultimately it is
  96. nothing but syntactic sugar. But then, what isn't?
  97.  
  98. Cheers
  99. Tanmoy
  100. --
  101. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  102. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  103. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  104. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  105. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  106. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  107.